package org.jeecg.modules.paycenter.service;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.paycenter.enums.BillTypeEnum;
import org.jeecg.modules.paycenter.enums.UTypeEnum;
import org.jeecg.modules.paycenter.dto.PayBillData;
import org.jeecg.modules.wlhy.bill.entity.*;
import org.jeecg.modules.wlhy.bill.service.*;
import org.jeecg.modules.wlhy.company.entity.HyCompany;
import org.jeecg.modules.wlhy.company.service.IHyCompanyService;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrier;
import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hydriverbank.entity.HyDriverBank;
import org.jeecg.modules.wlhy.hydriverbank.service.IHyDriverBankService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.CodeUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class BillService {
    @Autowired private IHyShippingNoteService hyShippingNoteService;
    @Autowired private IHyDriverService hyDriverService;
    @Autowired private IHyCarrierService hyCarrierService;
    @Autowired private IHyVehicleService hyVehicleService;
    @Autowired private IHyCompanyService hyCompanyService;

    @Autowired private IHyCustomerBillService hyCustomerBillService;
    @Autowired private IHyShippingNoteBillService hyShippingNoteBillService;
    @Autowired private IHyDriverBillService hyDriverBillService;
    @Autowired private IHyCarrierBillService hyCarrierBillService;
    @Autowired private IHyBillService hyBillService;
    @Autowired private IHyDriverBankService hyDriverBankService;
    @Autowired private IHySettingService settingService;

    //region base
    public BigDecimal getBillCustomerTotalFee(List<HyBill> hyBills){
        BigDecimal totalAmt = new BigDecimal("0");
        for (HyBill hyBill : hyBills) {
            totalAmt = totalAmt.add(hyBill.getCustomerAmt());
        }
        return totalAmt;
    }

    //获取分公司信息
    public Map<String, HyCompany> getCompanyMaps(){
        List<HyCompany> companyList = hyCompanyService.list();
        if(companyList == null || companyList.size()==0){
            throw new JeecgBootException("分公司信息不存在");
        }

        Map<String,HyCompany> companyMaps = new HashMap<>();
        for (HyCompany hyCompany : companyList) {
            companyMaps.put(hyCompany.getId(),hyCompany);
        }
        return companyMaps;
    }

    public Map<String, HyCompany> getCompanyMaps(List<HyShippingNote> shippingNotes){
        Set<String> companySet = new HashSet<>();
        for (HyShippingNote hyShippingNote : shippingNotes) {
            companySet.add(hyShippingNote.getCompanyId());
        }
        List<HyCompany> companyList = hyCompanyService.listByIds(companySet);
        if(companyList.size() != companySet.size()){
            throw new JeecgBootException("分公司信息不存在");
        }

        Map<String,HyCompany> companyMaps = new HashMap<>();
        for (HyCompany hyCompany : companyList) {
            companyMaps.put(hyCompany.getId(),hyCompany);
        }
        return companyMaps;
    }

    public Map<String, HyDriver> getDriverMaps(List<HyShippingNote> shippingNotes) {
        Set<String> driverSet = new HashSet<>();
        Map<String, HyDriver> driverMaps = new HashMap<>(); //司机

        for (HyShippingNote hyShippingNote : shippingNotes) {
            driverSet.add(hyShippingNote.getDriverId());
        }
        if (driverSet.size() > 0) {
            List<HyDriver> hyDrivers = hyDriverService.listByIds(driverSet);
            if (hyDrivers.size() != driverSet.size()) {
                throw new JeecgBootException("司机信息不存在");
            }
            for (HyDriver hyDriver : hyDrivers) {
                driverMaps.put(hyDriver.getId(), hyDriver);
            }
        }
        return driverMaps;
    }

    public Map<String, HyVehicle> getVehicleMaps(List<HyShippingNote> shippingNotes) {
        Set<String> vehicleSet = new HashSet<>();
        Map<String, HyVehicle> map = new HashMap<>();
        for (HyShippingNote hyShippingNote : shippingNotes) {
            vehicleSet.add(hyShippingNote.getVehicleId());
        }
        if(vehicleSet.size() > 0) {
            List<HyVehicle> hyVehicles = hyVehicleService.listByIds(vehicleSet);
            if (hyVehicles.size() != vehicleSet.size()) {
                throw new JeecgBootException("车辆信息不存在");
            }
            for (HyVehicle hyVehicle : hyVehicles) {
                map.put(hyVehicle.getId(), hyVehicle);
            }
        }
        return map;
    }

    public Map<String, HyCarrier> getCarrierMaps(List<HyShippingNote> shippingNotes) {
        Set<String> carrierSet = new HashSet<>();
        for (HyShippingNote hyShippingNote : shippingNotes) {
            if ("2".equals(hyShippingNote.getPayType())) {
                carrierSet.add(hyShippingNote.getCarrierId());
            }
        }
        Map<String, HyCarrier> carrierMaps = new HashMap<>();//车队长
        if (carrierSet.size() > 0) {
            List<HyCarrier> hyCarriers = hyCarrierService.listByIds(carrierSet);
            if (hyCarriers.size() != carrierSet.size()) {
                throw new JeecgBootException("车队长信息不存在");
            }
            for (HyCarrier hyCarrier : hyCarriers) {
                carrierMaps.put(hyCarrier.getId(), hyCarrier);
            }
        }
        return carrierMaps;
    }

    public Map<String, HyShippingNote> shippingListToMap(List<HyShippingNote> hyShippingNotes) {
        Map<String, HyShippingNote> shippingNoteMap = new HashMap<>();
        for (HyShippingNote hyShippingNote : hyShippingNotes) {
            shippingNoteMap.put(hyShippingNote.getId() , hyShippingNote);
        }
        return shippingNoteMap;
    }

    public List<HyShippingNote> getBillShippingList(List<HyBill> hyBills) {
        Set<String> shippingIds = new HashSet<>();
        for (HyBill hyBill : hyBills) {
            shippingIds.add(hyBill.getShippingId());
        }
        List<HyShippingNote> shippingNotes = hyShippingNoteService.listByIds(shippingIds);
        if(shippingNotes.size() != shippingIds.size()){
            throw new JeecgBootException("运单信息错误");
        }
        return shippingNotes;
    }

    public Map<String, HyCustomerBill>  getCustomerBillMap(List<HyCustomerBill> hyCustomerBills){
        Map<String,HyCustomerBill> customerBillMap = new HashMap<>();
        for (HyCustomerBill hyCustomerBill : hyCustomerBills) {
            customerBillMap.put(hyCustomerBill.getId() , hyCustomerBill);
        }
        return customerBillMap;
    }

    public Map<String, HyShippingNoteBill>  getShippingNoteBillMap(List<HyShippingNoteBill> shippingNoteBills){
        Map<String,HyShippingNoteBill> shippingNoteBillMap = new HashMap<>();
        for (HyShippingNoteBill shippingNoteBill : shippingNoteBills) {
            shippingNoteBillMap.put(shippingNoteBill.getId() , shippingNoteBill);
        }
        return shippingNoteBillMap;
    }

    public Map<String, HyDriverBill>  getDriverBillMap(List<HyDriverBill> hyDriverBills){
        Map<String,HyDriverBill> driverBillMap = new HashMap<>();
        for (HyDriverBill hyDriverBill : hyDriverBills) {
            driverBillMap.put(hyDriverBill.getId() , hyDriverBill);
        }
        return driverBillMap;
    }

    public Map<String, HyCarrierBill>  getCarrierBillMap(List<HyCarrierBill> hyCarrierBills){
        Map<String,HyCarrierBill> billMap = new HashMap<>();
        for (HyCarrierBill bill : hyCarrierBills) {
            billMap.put(bill.getId() , bill);
        }
        return billMap;
    }

    public Map<String, HyDriverBank> getDriverBankMaps(List<HyShippingNote> hyShippingNotes) {
        Map<String, HyDriverBank> driverBankMaps = new HashMap<>();
        Set<String> driverBankIdSet = new HashSet<>();
        for (HyShippingNote hyShippingNote : hyShippingNotes) {
            String driverBankId = hyShippingNote.getDriverBankId();
            if (StringUtils.isNotEmpty(driverBankId)) {
                driverBankIdSet.add(driverBankId);
            }
        }
        if (driverBankIdSet.size() > 0) {
            List<HyDriverBank> hyDriverBanks = hyDriverBankService.listByIds(driverBankIdSet);
            for (HyDriverBank hyDriverBank : hyDriverBanks) {
                driverBankMaps.put(hyDriverBank.getId(), hyDriverBank);
            }
        }
        return driverBankMaps;
    }

    public BigDecimal getCustomerCostPayTotal(HyShippingNote hyShippingNote){
        String orderMode = hyShippingNote.getOrderMode();
        //差价模式
        if ("3".equals(orderMode)) {
            return hyShippingNote.getAgentCostPayTotal();//运费合计
        } else if ("1".equals(orderMode)) {
            return hyShippingNote.getCostPayTotal();//运费合计
        } else if("4".equals(orderMode)) {
            return hyShippingNote.getCostPayTotal();//运费合计
        }else{
            throw new JeecgBootException("运单管理模式信息错误");
        }
    }

    /**
     * carrierFeeType车队长运费计算方式(0全部1按吨)
     * carrierFeeType = 0  全部 : 返回实付运费 costPayShipping
     * carrierFeeType = 1  按吨 : 返回车队长运费 carrierFee
     * @param hyShippingNote
     * @return
     */
    public BigDecimal getCarrierBillAmt(HyShippingNote hyShippingNote) {
        //carrierFeeType车队长运费计算方式(0全部1按吨2包车价)
        if ("1".equals(hyShippingNote.getCarrierFeeType()) || "2".equals(hyShippingNote.getCarrierFeeType())) {
            return hyShippingNote.getCarrierFee();
        } else {
            return hyShippingNote.getCostPayShipping();
        }
    }

    public String getHyBillCustomerId(List<HyBill> hyBills) {
        if(hyBills == null || hyBills.size() == 0){
            throw new JeecgBootException("支付订单不存在");
        }
        String customerId = hyBills.get(0).getCustomerId();
        for (HyBill hyBill : hyBills) {
            if(!StringUtils.equals(customerId, hyBill.getCustomerId())){
                throw new JeecgBootException("支付订单信息错误");
            }
        }
        return customerId;
    }

    //endregion

    //region 支付订单检查
    public Result<?> checkBills(List<HyBill> hyBills, String customerId) {
        for (HyBill hyBill : hyBills) {
            if ("5".equals(hyBill.getPayStatus())) {
                return Result.error("已支付，不能重复支付!");
            }
            if (!"4".equals(hyBill.getPayStatus())) {
                return Result.error("未审核通过,不能支付!");
            }
            if (!StringUtils.equals(customerId, hyBill.getCustomerId())) {
                return Result.error("数据错误!");
            }
        }
        return Result.OK(customerId);
    }

    public void checkBills(List<HyBill> hyBills, Map<String, HyShippingNote> shippingNoteMap) {
        for (HyBill hyBill : hyBills) {
            String billType = hyBill.getBillType();
            if(BillTypeEnum.PLAN_PAY_DAOFU.value.equals(billType)) {
                HyShippingNote hyShippingNote = shippingNoteMap.get(hyBill.getShippingId());
                if (hyShippingNote == null) {
                    throw new JeecgBootException("运单信息不存在");
                }
                String shippingStatus = hyShippingNote.getShippingStatus();
                if (!"4".equals(shippingStatus) && !"5".equals(shippingStatus)) {
                    throw new JeecgBootException(String.format("运单%s未交货,不能支付到付款", hyShippingNote.getShippingNoteNumber()));
                }
                String shippingPayStatus = hyShippingNote.getShippingPayStatus();
                if ("0".equals(shippingPayStatus) || "9".equals(shippingPayStatus) || "10".equals(shippingPayStatus)) {
                    throw new JeecgBootException(String.format("运单%s业务审核未通过,不能支付到付款", hyShippingNote.getShippingNoteNumber()));
                }
            }
            if(BillTypeEnum.PAY.value.equals(billType)) {
                HyShippingNote hyShippingNote = shippingNoteMap.get(hyBill.getShippingId());
                if (hyShippingNote == null) {
                    throw new JeecgBootException("运单信息不存在");
                }
                if("1".equals(hyShippingNote.getIzUsedPayPlan())){
                    BigDecimal costPay2 = hyShippingNote.getCostPay2();
                    if(costPay2 != null && costPay2.compareTo(BigDecimal.ZERO) > 0){
                        //检查尾款是否已支付
                        HyBill dfbill = hyBillService.getBillByShippingId(hyBill.getShippingId(), BillTypeEnum.PLAN_PAY_DAOFU.value);
                        if(dfbill == null || !"6".equals(dfbill.getPayStatus())){
                            throw new JeecgBootException(String.format("运单%s尾款未支付,不能支付尾款", hyShippingNote.getShippingNoteNumber()));
                        }
                    }
                }
            }
        }
    }

    public Result<?> checkBills(List<HyBill> hyBills) {
        String customerId = hyBills.get(0).getCustomerId();
        for (HyBill hyBill : hyBills) {
            if ("5".equals(hyBill.getPayStatus())) {
                return Result.error("已支付，不能重复支付!");
            }
            if (!"4".equals(hyBill.getPayStatus())) {
                return Result.error("未审核通过,不能支付!");
            }
            if (!StringUtils.equals(customerId, hyBill.getCustomerId())) {
                return Result.error("不是同一个货主，不能批量支付!");
            }
        }
        return Result.OK(customerId);
    }

    public Result<?> checkAgentBills(List<HyBill> hyBills,String agentId) {
        String customerId = hyBills.get(0).getCustomerId();
        for (HyBill hyBill : hyBills) {
            if ("5".equals(hyBill.getPayStatus())) {
                return Result.error("已支付，不能重复支付!");
            }
            if (!"4".equals(hyBill.getPayStatus())) {
                return Result.error("未审核通过,不能支付!");
            }
            if (!StringUtils.equals(hyBill.getAgentId(), agentId)) {
                return Result.error("运单数据错误!");
            }
            if (!StringUtils.equals(customerId, hyBill.getCustomerId())) {
                return Result.error("不是同一个货主不能支付!");
            }
        }
        return Result.OK(customerId);
    }

    public String checkHyBillCustomerId(List<HyBill> hyBills , String customerId) {
        if(hyBills == null || hyBills.size() == 0){
            throw new JeecgBootException("支付订单不存在");
        }
        for (HyBill hyBill : hyBills) {
            if(!StringUtils.equals(customerId, hyBill.getCustomerId())){
                throw new JeecgBootException("支付订单信息错误");
            }
        }
        return customerId;
    }

    //endregion

    //region 创建支付订单
    /**
     *
     * @param hyShippingNotes
     * @param billType
     * @param subAmt  分段预付专用参数
     * @return
     */
    public List<HyBill> createBills(List<HyShippingNote> hyShippingNotes, BillTypeEnum billType , BigDecimal subAmt) {
        List<HyBill> hyBills = new ArrayList<>();
        String isAut = settingService.getSettingValueByTag2("isshippingnoteAut");
        for (HyShippingNote shippingNote : hyShippingNotes) {
            HyBill bill = this.createBill(shippingNote, billType, subAmt , isAut);
            hyBills.add(bill);
        }
        return hyBills;
    }

    /**
     *
     * @param shippingNote
     * @param billType
     * @param subAmt 分段预付专用参数
     * @return
     */
    public HyBill createBill(HyShippingNote shippingNote, BillTypeEnum billType, BigDecimal subAmt ,String isAut) {
        HyBill bill = new HyBill();
        bill.setBillType(billType.value);
        bill.setShippingId(shippingNote.getId());
        bill.setCustomerId(shippingNote.getCustomerId());
        bill.setShippingStatus(shippingNote.getShippingStatus());
        if("3".equals(shippingNote.getOrderMode())) {
            bill.setAgentId(shippingNote.getAgentId());
        }
        if("1".equals(billType.value)) {//支付运单、尾款
            BigDecimal customerAmt = this.getCustomerCostPayTotal(shippingNote);//运费合计（运费+服务费），货主支付给分公司
            BigDecimal driverAmt = shippingNote.getCostPayShipping();
            bill.setAmt(customerAmt);
            bill.setCustomerAmt(customerAmt);
            bill.setDriverAmt(driverAmt);
            if("2".equals(shippingNote.getPayType())) {//车队长运费
                BigDecimal carrierFee = this.getCarrierBillAmt(shippingNote);
                bill.setCarrierAmt(carrierFee);
                bill.setIzCarrierPay("1");
            }
        }else if("6".equals(billType.value)){//预付运费
            BigDecimal amt = shippingNote.getPreFee();
            bill.setAmt(amt);
            bill.setCustomerAmt(amt);
            bill.setDriverAmt(amt);
        }else if("7".equals(billType.value)){//分段预付运费
            bill.setAmt(subAmt);
            bill.setCustomerAmt(subAmt);
            bill.setDriverAmt(subAmt);
            if("2".equals(shippingNote.getPayType())) {
                //车队长运费
                bill.setCarrierAmt(subAmt);
                bill.setIzCarrierPay("1");
            }
        }else if("8".equals(billType.value)){//支付计划首付
            BigDecimal amt = shippingNote.getCostPay1();
            bill.setAmt(amt);
            bill.setCustomerAmt(amt);
            bill.setDriverAmt(amt);
            if("2".equals(shippingNote.getPayType())) {
                //车队长运费
                bill.setCarrierAmt(amt);
                bill.setIzCarrierPay("1");
            }
        }else if("9".equals(billType.value)){//支付计划到付
            BigDecimal amt = shippingNote.getCostPay2();
            bill.setAmt(amt);
            bill.setCustomerAmt(amt);
            bill.setDriverAmt(amt);
            if("2".equals(shippingNote.getPayType())) {
                //车队长运费
                bill.setCarrierAmt(amt);
                bill.setIzCarrierPay("1");
            }
        }else {
            throw new JeecgBootException("支付订单类型错误");
        }
//        bill.setPayStatus("4");//待支付
        if("1".equals(isAut)) {
            //审核
            bill.setPayStatus("3");//待审核
        }else {
            //直接开始支付
            bill.setPayStatus("4");//待支付
        }
        return bill;
    }

    //endregion

    //region 创建支付订单的资金流水单、运费流水单
    public HyCustomerBill createBillCustomerBill(HyBill bill, HyShippingNote hyShippingNote) {
        String customerBillNum = CodeUtil.getBillNum(UTypeEnum.CUSTOMER);
        HyCustomerBill customerBill = new HyCustomerBill();
        customerBill.setBillId(bill.getId());
        customerBill.setBillNum(customerBillNum);//设置支付流水单
        customerBill.setBillType(bill.getBillType());
        customerBill.setOrderId(hyShippingNote.getOrderId());
        customerBill.setShippingId(hyShippingNote.getId());
        customerBill.setCustomerId(hyShippingNote.getCustomerId());
        customerBill.setCompanyId(hyShippingNote.getCompanyId());
        customerBill.setPayDt(new Date());
        customerBill.setPayChannel(bill.getPayChannel());
        customerBill.setPaySource(bill.getPaySource());

        BigDecimal customerAmt = bill.getCustomerAmt();
        customerBill.setAmt(customerAmt);

        if("1".equals(bill.getBillType())) {
            customerBill.setCostPayShipping(bill.getDriverAmt());
            customerBill.setCostService(hyShippingNote.getCostService());
        }else {
            customerBill.setCostPayShipping(customerAmt);
            customerBill.setCostService(BigDecimal.ZERO);
        }
        return customerBill;
    }

    public HyShippingNoteBill createBillShippingBill(HyBill bill,HyShippingNote hyShippingNote , HyDriver driver , HyVehicle vehicle) {
        String shippingNoteBillNum = CodeUtil.getBillNum(UTypeEnum.DRIVER);
        //添加运单支付流水单,分公司到司机
        HyShippingNoteBill shippingNoteBill = new HyShippingNoteBill();
        shippingNoteBill.setBillId(bill.getId());
        shippingNoteBill.setBillNum(shippingNoteBillNum);
        shippingNoteBill.setBillType(bill.getBillType());
        shippingNoteBill.setOrderId(hyShippingNote.getOrderId());
        shippingNoteBill.setShippingId(hyShippingNote.getId());
        shippingNoteBill.setShippingNoteNumber(hyShippingNote.getShippingNoteNumber());
        shippingNoteBill.setCompanyId(hyShippingNote.getCompanyId());
        shippingNoteBill.setPayType(hyShippingNote.getPayType());
        shippingNoteBill.setCarrierId(hyShippingNote.getCarrierId());
        shippingNoteBill.setDriverId(hyShippingNote.getDriverId());
        shippingNoteBill.setCarrier(driver.getDriverName());
        shippingNoteBill.setActualCarrierId(driver.getIdcard());
        shippingNoteBill.setVehicleId(hyShippingNote.getVehicleId());
        shippingNoteBill.setVehicleNumber(vehicle.getVehicleNumber());
        shippingNoteBill.setVehicleColorCode(vehicle.getVehiclePlateColorCode());
        shippingNoteBill.setPayDt(new Date());//支付时间
        shippingNoteBill.setPayChannel(bill.getPayChannel());
        shippingNoteBill.setAmt(bill.getDriverAmt());
        return shippingNoteBill;
    }

    public HyDriverBill createBillDriverBill(HyBill bill,HyShippingNote hyShippingNote , HyShippingNoteBill shippingNoteBill) {
        //添加司机流水单
        HyDriverBill driverBill = new HyDriverBill();
        driverBill.setBillId(bill.getId());
        driverBill.setBillType(bill.getBillType());
        driverBill.setBillNum(shippingNoteBill.getBillNum());//流水单号
        driverBill.setOrderId(hyShippingNote.getOrderId());
        driverBill.setCompanyId(hyShippingNote.getCompanyId());
        driverBill.setShippingId(hyShippingNote.getId());
        driverBill.setDriverId(hyShippingNote.getDriverId());
        driverBill.setPayChannel(bill.getPayChannel());
        driverBill.setAmt(bill.getDriverAmt());
        return driverBill;
    }

    public HyCarrierBill createBillCarrierBill(HyBill bill, HyShippingNote hyShippingNote) {
        String carrierBillNum = CodeUtil.getBillNum(UTypeEnum.CARRIER);
        HyCarrierBill carrierBill = new HyCarrierBill();
        carrierBill.setBillId(bill.getId());
        carrierBill.setBillNum(carrierBillNum);
        carrierBill.setBillType("1");
        carrierBill.setCarrierId(hyShippingNote.getCarrierId());
        carrierBill.setOrderId(hyShippingNote.getOrderId());
        carrierBill.setCompanyId(hyShippingNote.getCompanyId());
        carrierBill.setShippingId(hyShippingNote.getId());
        carrierBill.setDriverId(hyShippingNote.getDriverId());
        carrierBill.setPayChannel(bill.getPayChannel());
        carrierBill.setAmt(bill.getCarrierAmt());
        return carrierBill;
    }

    public PayBillData createBillPayData(HyBill hyBill , HyShippingNote hyShippingNote , HyDriver driver, HyVehicle vehicle) {
        PayBillData payBillData = new PayBillData();
        HyCustomerBill customerBill = this.createBillCustomerBill(hyBill, hyShippingNote);
        HyShippingNoteBill shippingNoteBill = this.createBillShippingBill(hyBill, hyShippingNote, driver, vehicle);
        //添加司机流水单
        HyDriverBill driverBill = this.createBillDriverBill(hyBill, hyShippingNote, shippingNoteBill);
        //是否车队长代收
        if ("1".equals(hyBill.getIzCarrierPay())) {
            HyCarrierBill carrierBill = this.createBillCarrierBill(hyBill, hyShippingNote);
            payBillData.setCarrierBill(carrierBill);
        }
        payBillData.setHyBill(hyBill);
        payBillData.setShippingNote(hyShippingNote);
        payBillData.setCustomerBill(customerBill);
        payBillData.setShippingNoteBill(shippingNoteBill);
        payBillData.setDriverBill(driverBill);
        return payBillData;
    }

    public List<PayBillData> createBillPayDatas(String channel,List<HyBill> hyBills,
                                                Map<String, HyShippingNote> shippingNoteMap,
                                                Map<String, HyDriver> driverMaps,
                                                Map<String, HyVehicle> vehicleMaps) {
        List<PayBillData> payBillDatas = new ArrayList<>();
        String realName = UserUtil.getLoginUser().getRName();
        for (HyBill hyBill : hyBills) {
            hyBill.setBillPayStatus("1");//开始支付
            hyBill.setPayStatus("5");
            hyBill.setPayChannel(channel);
            hyBill.setPayDt(new Date());//申请运费支付时间
            hyBill.setFkr(realName);
            HyShippingNote hyShippingNote = shippingNoteMap.get(hyBill.getShippingId());
            HyDriver driver = driverMaps.get(hyShippingNote.getDriverId());
            HyVehicle vehicle = vehicleMaps.get(hyShippingNote.getVehicleId());
            PayBillData payBillData = this.createBillPayData(hyBill, hyShippingNote, driver, vehicle);
            payBillDatas.add(payBillData);
        }
        return payBillDatas;
    }

    @Transactional
    public void saveBillPayDatas(List<HyBill> hyBills ,List<PayBillData> payBillDatas) {
        List<HyCustomerBill> customerBills = new ArrayList<>();
        List<HyShippingNoteBill> shippingNoteBills = new ArrayList<>();
        List<HyDriverBill> driverBills = new ArrayList<>();
        List<HyCarrierBill> carrierBills = new ArrayList<>();
        for (PayBillData payBillData : payBillDatas) {
            customerBills.add(payBillData.getCustomerBill());
            shippingNoteBills.add(payBillData.getShippingNoteBill());
            driverBills.add(payBillData.getDriverBill());
            HyCarrierBill carrierBill = payBillData.getCarrierBill();
            if (carrierBill != null) {
                carrierBills.add(carrierBill);
            }
        }
        if (customerBills.size() > 0) {
            hyCustomerBillService.saveBatch(customerBills);
        }
        if (shippingNoteBills.size() > 0) {
            hyShippingNoteBillService.saveBatch(shippingNoteBills);
        }
        if (driverBills.size() > 0) {
            hyDriverBillService.saveBatch(driverBills);
        }
        if (carrierBills.size() > 0) {
            hyCarrierBillService.saveBatch(carrierBills);
        }

        for (PayBillData payBillData : payBillDatas) {
            HyBill hyBill = payBillData.getHyBill();
            HyCustomerBill customerBill = payBillData.getCustomerBill();
            HyShippingNoteBill shippingNoteBill = payBillData.getShippingNoteBill();
            HyDriverBill driverBill = payBillData.getDriverBill();
            hyBill.setCustomerBillId(customerBill.getId());
            hyBill.setShippingBillId(shippingNoteBill.getId());
            hyBill.setDriverBillId(driverBill.getId());

            HyCarrierBill carrierBill = payBillData.getCarrierBill();
            if (carrierBill != null) {
                hyBill.setCarrierBillId(carrierBill.getId());
            }
        }

        hyBillService.updateBatchById(hyBills);
    }

    public List<PayBillData> getBillRePayDatas(List<HyBill> hyBills) {
        List<String> shippingIds = new ArrayList<>();
        List<String> customerBillIds = new ArrayList<>();
        List<String> shippingBillIds = new ArrayList<>();
        List<String> driverBillIds = new ArrayList<>();
        List<String> carrierBillIds = new ArrayList<>();
        for (HyBill hyBill : hyBills) {
            shippingIds.add(hyBill.getShippingId());
            customerBillIds.add(hyBill.getCustomerBillId());
            shippingBillIds.add(hyBill.getShippingBillId());
            driverBillIds.add(hyBill.getDriverBillId());
            if("1".equals(hyBill.getIzCarrierPay())){
                carrierBillIds.add(hyBill.getCarrierBillId());
            }
        }
        int size = hyBills.size();
        List<HyShippingNote> shippingNotes = hyShippingNoteService.listByIds(shippingIds);
        if(shippingIds.size() != shippingNotes.size()){
            throw new JeecgBootException("支付数据错误");
        }
        List<HyCustomerBill> hyCustomerBills = hyCustomerBillService.listByIds(customerBillIds);
        if(size != hyCustomerBills.size()){
            throw new JeecgBootException("支付数据错误");
        }
        List<HyShippingNoteBill> shippingNoteBills = hyShippingNoteBillService.listByIds(shippingBillIds);
        if(size != shippingNoteBills.size()){
            throw new JeecgBootException("支付数据错误");
        }
        List<HyDriverBill> hyDriverBills = hyDriverBillService.listByIds(driverBillIds);
        if(size != hyDriverBills.size()){
            throw new JeecgBootException("支付数据错误");
        }

        Map<String, HyCarrierBill> carrierBillMap = null;
        if(carrierBillIds.size() > 0){
            List<HyCarrierBill> hyCarrierBills = hyCarrierBillService.listByIds(carrierBillIds);
            if(carrierBillIds.size() != hyCarrierBills.size()){
                throw new JeecgBootException("支付数据错误");
            }
            carrierBillMap = this.getCarrierBillMap(hyCarrierBills);
        }
        Map<String, HyShippingNote> shippingNoteMap = this.shippingListToMap(shippingNotes);
        Map<String, HyCustomerBill> customerBillMap = this.getCustomerBillMap(hyCustomerBills);
        Map<String, HyShippingNoteBill> shippingNoteBillMap = this.getShippingNoteBillMap(shippingNoteBills);
        Map<String, HyDriverBill> driverBillMap = this.getDriverBillMap(hyDriverBills);

        List<PayBillData> payBillDatas = new ArrayList<>();
        for (HyBill hyBill : hyBills) {
            HyShippingNote shippingNote = shippingNoteMap.get(hyBill.getShippingId());
            HyCustomerBill customerBill = customerBillMap.get(hyBill.getCustomerBillId());
            HyShippingNoteBill shippingNoteBill = shippingNoteBillMap.get(hyBill.getShippingBillId());
            HyDriverBill driverBill = driverBillMap.get(hyBill.getDriverBillId());
            PayBillData payBillData = new PayBillData();
            payBillData.setShippingNote(shippingNote);
            payBillData.setHyBill(hyBill);
            payBillData.setCustomerBill(customerBill);
            payBillData.setShippingNoteBill(shippingNoteBill);
            payBillData.setDriverBill(driverBill);
            if("1".equals(hyBill.getIzCarrierPay())){
                HyCarrierBill carrierBill = carrierBillMap.get(hyBill.getCarrierBillId());
                payBillData.setCarrierBill(carrierBill);
            }
            payBillDatas.add(payBillData);
        }
        return payBillDatas;
    }


    //endregion
}
